package com.ylyl.langchain4j06chatimage.controller;


import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Base64;

@Slf4j
@RestController
public class ImageModelController {

    @Autowired
    private ChatModel chatModel;

    @Value("classpath:static/images/mv.png")
    private Resource resources;

    @GetMapping(value = "/images/call")
    public String readImageContent() throws IOException {
        byte[] byteArray = resources.getContentAsByteArray();
        String base6Data = Base64.getEncoder().encodeToString(byteArray);
        UserMessage userMessage = UserMessage.from(
                TextContent.from("以下女明星是叫什么名字"),
                ImageContent.from(base6Data, "image/png")
        );

        ChatResponse chatResponse = chatModel.chat(userMessage);
        String result = chatResponse.aiMessage().text();

        System.out.println(result);

        return result;
    }
}
